package com.lst.quartz;

import com.lst.quartz.job.FirstJob;
import org.quartz.*;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.Resource;

/**
 * @author shengtao
 * @Description:
 * @date 2018/12/12 11:47
 */
@SpringBootApplication
public class Application implements CommandLineRunner,ApplicationContextAware{

    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }


    @Override
    public void run(String... args) throws Exception {
        // 启动调度器
        Scheduler scheduler = applicationContext.getBean(Scheduler.class);

        JobDetail jobDetail = JobBuilder.newJob(FirstJob.class).withIdentity("firs", "12").build();
        //表达式调度构建器(即任务执行的时间)
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");

        //按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(FirstJob.class.getName(), "12")
                .withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
